#!/bin/bash

function quit() {
    ./util/error.sh "$title" "$1"
    exit 1
}

function checkInstall() {
    name=$1
    path=`which $name`
    if [ -z $path ]; then
        quit "not install"
    fi
}

function checkDir() {
    dir=$1
    if [ ! -d "$dir" ]; then
        quit "dir not exist"
    fi
}


###########################################################################

cd $(dirname $(readlink -f $0));

title="check git"
checkInstall git
checkDir "`./config/git_path.sh`"
./util/info.sh "$title" "OK"


title="check php"
checkInstall php
./util/info.sh "$title" "OK"


title="check node"
checkInstall node
./util/info.sh "$title" "OK"


title="check mongo"
checkInstall mongo
mongo `./config/get.sh merge.mongo_address` --quiet --eval "quit()"
if [ $? -ne 0 ]; then
    quit "config.merge invalid"
fi
./util/info.sh "$title" "OK"


title="check mysql"
checkInstall mysql
mysql_ip=`./config/get.sh mysql.ip`
mysql_port=`./config/get.sh mysql.port`
mysql_user=`./config/get.sh mysql.user`
mysql_password=`./config/get.sh mysql.password`
mysql -h"$mysql_ip" -P$mysql_port -u"$mysql_user" -p"$mysql_password" -e ""
if [ $? -ne 0 ]; then
    quit 1 "config.mysql invalid"
fi
./util/info.sh "$title" "OK"

./util/notice.sh "check" "Done"
